﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZRFCore.MotionControlCard;
using ZRFCore.Tool;
using System.IO;
using MultiAxisVisionAssembly.VisionAlgorithm;

namespace MultiAxisVisionAssembly.Motion
{
    public class MachinePro : Machine
    {
        public double ZWorkPos { get; set; }//雾化头与板面接触的Z轴高度位置
        public double ZSmallWorkPos { get; set; }//点胶头伸出来时与板面接触高度
        public double ZSafePos { get; set; }//Z轴安全高度位

        public IInterpolation AxisesInterpolation { get { return interpolation; } }

        /// <summary>
        /// 初始化完成
        /// </summary>
        public new bool IsInitialized;

        public MachinePro()
        {
            IsInitialized = false;
        }

        public new void Init()
        {
            try
            {
                base.Init();
                base.In_EmergentStopButton.DisableEvent += In_EmergentStopButton_DisableEvent1;
                In_EmergentStopButton_DisableEvent1(null, null);
                IsInitialized = true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        //急停输入断开事件处理器，触发设备急停
        private void In_EmergentStopButton_DisableEvent1(IInput? sender, InputEventArgs? e)
        {
            if (In_EmergentStopButton.Enable == false)
            {
                EmergentStop();
            }
        }

        public new void EmergentStop()
        {
            Out_GlueControl.Enable = false;
            Out_PointGlueControl.Enable = false;
            base.EmergentStop();
        }

        public new void ResetEmergentStop()
        {
            if (In_EmergentStopButton.Enable == false)
            {
                throw new Exception("未解除急停按钮！");
            }

            base.ResetEmergentStop();
        }

        public new void Close()
        {
            EmergentStop();
            base.Close();
        }

        /// <summary>
        /// 去拍照位
        /// </summary>
        /// <param name="XSnapPos"></param>
        /// <param name="YSnapPos"></param>
        /// <param name="ZSnapPos"></param>
        public void GotoSnapPos(float XSnapPos, float YSnapPos, float ZSnapPos)
        {
            base.Speed = base.MaxSpeed;//胶头平移速度
            base.Accel = base.MaxSpeed * 5;
            base.Decel = base.MaxSpeed * 5;
            if (Math.Abs(this.ZAxis.DPos) > Math.Abs(ZSafePos))//判断当前Z轴高度是否低于安全高度
            {
                if (this.MotionStatus.EmergentStop == true)
                {
                    throw new Exception("急停中");
                }
                this.ZAxis.MoveAbs((float)ZSafePos);
                this.ZAxis.WaitMoveStop();
            }
            List<double[]> snapPosList = new List<double[]>();
            int delayTime;
            if (this.XAxis.DPos == XSnapPos && this.YAxis.DPos == YSnapPos)
            {
                delayTime = 5;
            }
            else
            {
                delayTime = 1500;
            }
            snapPosList.Add(new double[] { XSnapPos, YSnapPos, ZSnapPos });
            this.Interpolation.MoveAbs(snapPosList);//移动到拍照位
            this.Interpolation.WaitMoveAbsOver();//
            Thread.Sleep(delayTime);
        }

        /// <summary>
        /// 执行点胶动作
        /// </summary>
        /// <param name="machPath"></param>
        /// <param name="needOutGlue"></param>
        //public void DoMachCoordPath(MachCoord2dPath machPath, bool needOutGlue = true)
        //{
        //    if (machPath.Shield == true)
        //    {
        //        return;
        //    }
        //    base.Speed = base.MaxSpeed;//胶头平移速度
        //    base.Accel = base.MaxSpeed * 5;
        //    base.Decel = base.MaxSpeed * 5;

        //    double zWorkPos;
        //    if (machPath.Type == GluePathType.SmallContinuousLine)
        //    {
        //        Out_GlueChangeToSmall.Enable = true;
        //        zWorkPos = ZSmallWorkPos;
        //    }
        //    else
        //    {
        //        Out_GlueChangeToSmall.Enable = false;
        //        zWorkPos = ZWorkPos;
        //    }

        //    //base.UnInterpolationCalcAxises = null;//new int[] { 3 };
        //    if (Math.Abs(base.ZPos) > Math.Abs(ZSafePos))//判断当前Z轴高度是否低于安全高度
        //    {
        //        if (base.MotionStatus == MotionStatus.EmergentStop)
        //        {
        //            return;
        //        }
        //        base.AbsMoveZAxis((float)ZSafePos);
        //        base.WaitZMoveStop();
        //    }
        //    List<double[]> moveToEnterPointPoses = new List<double[]>();
        //    //moveToEnterPointPoses.Add(new double[] { machPath.EnterX, machPath.EnterY, base.ZPos, machPath.EnterAngle });//移动到点胶进入点上方安全位置
        //    //moveToEnterPointPoses.Add(new double[] { machPath.EnterX, machPath.EnterY, ZWorkPos - upDist, machPath.EnterAngle });//移动到点胶进入点
        //    moveToEnterPointPoses.Add(new double[] { machPath.XPoses[0], machPath.YPoses[0], base.ZPos, machPath.RAngles[0] });//移动到点胶进入点上方安全位置
        //    //moveToEnterPointPoses.Add(new double[] { machPath.XPoses[0], machPath.YPoses[0], ZWorkPos - upDist, machPath.EnterAngle });//移动到点胶进入点
        //    moveToEnterPointPoses.Add(new double[] { machPath.XPoses[0], machPath.YPoses[0], zWorkPos - machPath.WorkHeight, machPath.RAngles[0] });//移动到点胶第一个点
        //    base.MoveAbs(moveToEnterPointPoses);//执行点胶前移动
        //    base.WaitMoveAbsOver();//等待运行延时结束
        //    Thread.Sleep((int)machPath.Delay_DelayToOutGlue);//到位延时时间
        //    //开始走点胶路径
        //    if (needOutGlue)
        //    {
        //        if (machPath.Type == GluePathType.SmallContinuousLine)
        //        {
        //            base.Out_PointGlueControl.Enable = true;//打开点胶阀
        //        }
        //        else
        //        {
        //            base.Out_GlueControl.Enable = true;//打开胶阀
        //        }
        //    }
        //    Thread.Sleep((int)machPath.Delay_OutGlueToStartMove);//
        //    if (machPath.Speed == 0)
        //    {
        //        base.Speed = 1;//点胶插补速度
        //    }
        //    else
        //    {
        //        base.Speed = (int)machPath.Speed;//点胶插补速度
        //    }
        //    base.UnInterpolationCalcAxises = new int[] { 3 };//R轴不参与插补计算
        //    List<double[]> moveAbs = new List<double[]>();
        //    for (int i = 1; i < machPath.XPoses.Length; i++)
        //    {
        //        if (base.MotionStatus == MotionStatus.EmergentStop)
        //        {
        //            return;
        //        }
        //        moveAbs.Add(new double[] { machPath.XPoses[i], machPath.YPoses[i], zWorkPos - machPath.WorkHeight, machPath.RAngles[i] });
        //    }
        //    base.MoveAbs(moveAbs);
        //    base.WaitMoveAbsOver();
        //    if (machPath.Type == GluePathType.SmallContinuousLine)
        //    {
        //        base.Out_PointGlueControl.Enable = false;//打开点胶阀
        //    }
        //    else
        //    {
        //        base.Out_GlueControl.Enable = false;//打开胶阀
        //    }
        //    base.UnInterpolationCalcAxises = null;//new int[] { 3 };//R轴参与插补计算
        //    //List<double[]> moveEnd = new List<double[]>();//点胶插补收尾
        //    //moveEnd.Add(new double[] { machPath.LastPointX, machPath.LastPointY, ZWorkPos - 5, base.RPos });
        //    //base.MoveAbs(moveEnd);//执行收尾插补
        //    //base.WaitMoveAbsOver();
        //    //Thread.Sleep((int)machPath.Delay_StopGlueDelayTime);
        //    //点胶结束出去
        //    base.Speed = base.MaxSpeed;//胶头平移速度
        //    base.Accel = base.MaxSpeed * 5;
        //    base.Decel = base.MaxSpeed * 5;
        //    //List<double[]> moveToEndPointPoses = new List<double[]>();
        //    //moveToEndPointPoses.Add(new double[] { machPath.XPoses.Last(), machPath.YPoses.Last(), ZWorkPos - upDist, base.RPos });
        //    //moveToEndPointPoses.Add(new double[] { machPath.XPoses.Last(), machPath.YPoses.Last(), ZWorkPos - upDist, machPath.EndAngle });//移动到点胶出去点
        //    //base.MoveAbs(moveToEndPointPoses);
        //    //base.WaitMoveAbsOver();
        //    base.AbsMoveZAxis((float)ZSafePos);//抬高Z轴
        //    base.WaitZMoveStop();
        //}
    }
}
